package com.imesne.office.excel.read;


import com.imesne.office.excel.model.ExcelRange;
import com.imesne.office.excel.utils.ExcelKitUtils;
import com.imesne.office.excel.validation.CellValueValidator;

import java.util.ArrayList;
import java.util.List;

/**
 * excel工具类配置
 * <p>
 * Created by liyd on 17/7/3.
 */
public class ExcelReaderConfig {

    private ExcelBookReader excelBookReader;

    private ExcelSheetReader excelSheetReader;

    private ExcelRowReader excelRowReader;

    private ExcelCellReader excelCellReader;

    /**
     * 首行是否标题
     */
    private boolean isFirstRowTitle;

    private List<CellValueValidator> cellValueValidators;

    private List<ExcelRange> excelRanges;

    private ExcelReadProcessor excelReadProcessor;

    public ExcelReaderConfig() {
        this.excelBookReader = new ExcelBookReaderImpl();
        this.excelSheetReader = new ExcelSheetReaderImpl();
        this.excelRowReader = new ExcelRowReaderImpl();
        this.excelCellReader = new ExcelCellReaderImpl();
        excelRanges = new ArrayList<>();
        cellValueValidators = new ArrayList<>();
        this.isFirstRowTitle = true;
    }

    /**
     * 添加读取范围
     *
     * @param excelRange
     */
    public void addRange(ExcelRange excelRange) {
        excelRanges.add(excelRange);
    }

    /**
     * 添加读取单元格
     *
     * @param sheetNum
     * @param x        行
     * @param y        列
     */
    public void addRangeCell(int sheetNum, int x, String y) {
        int yNum = ExcelKitUtils.columnToNum(y);
        addRangeCell(sheetNum, x, yNum);
    }

    /**
     * 添加读取单元格
     *
     * @param sheetNum
     * @param x        行
     * @param y        列
     */
    public void addRangeCell(int sheetNum, int x, int y) {
        ExcelRange range = ExcelRange.builder()
                .sheetNum(sheetNum)
                .beginRowNum(x)
                .endRowNum(x)
                .beginCellNum(y)
                .endCellNum(y)
                .build();
        excelRanges.add(range);
    }

    public void addCellValueValidator(CellValueValidator cellValueValidator) {
        this.cellValueValidators.add(cellValueValidator);
    }

    public ExcelBookReader getExcelBookReader() {
        initReader(excelBookReader);
        return excelBookReader;
    }

    public ExcelSheetReader getExcelSheetReader() {
        initReader(excelSheetReader);
        return excelSheetReader;
    }

    public ExcelRowReader getExcelRowReader() {
        initReader(excelRowReader);
        return excelRowReader;
    }

    public ExcelCellReader getExcelCellReader() {
        initReader(excelCellReader);
        return excelCellReader;
    }

    public List<CellValueValidator> getCellValueValidators() {
        return cellValueValidators;
    }

    public void setExcelBookReader(ExcelBookReader excelBookReader) {
        this.excelBookReader = excelBookReader;
    }

    public void setExcelSheetReader(ExcelSheetReader excelSheetReader) {
        this.excelSheetReader = excelSheetReader;
    }

    public void setExcelRowReader(ExcelRowReader excelRowReader) {
        this.excelRowReader = excelRowReader;
    }

    public void setExcelCellReader(ExcelCellReader excelCellReader) {
        this.excelCellReader = excelCellReader;
    }

    public boolean isFirstRowTitle() {
        return isFirstRowTitle;
    }

    public void setFirstRowTitle(boolean firstRowTitle) {
        isFirstRowTitle = firstRowTitle;
    }

    public List<ExcelRange> getExcelRanges() {
        return excelRanges;
    }

    public ExcelReadProcessor getExcelReadProcessor() {
        return excelReadProcessor;
    }

    public void setExcelReadProcessor(ExcelReadProcessor excelReadProcessor) {
        this.excelReadProcessor = excelReadProcessor;
    }

    private void initReader(Object reader) {
        if (reader instanceof AbstractConfigReader) {
            ((AbstractConfigReader) reader).setExcelReaderConfig(this);
        }
    }
}
